/*
* Copyright 2011 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/
package org.etherframework.wire
{
	import flash.errors.IllegalOperationError;
	import flash.utils.Dictionary;
	
	import org.etherframework.IEther;
	import org.etherframework.common.INotifiableWirable;
	import org.etherframework.core.reflection.MetadataMember;
	import org.etherframework.common.wire.IMetaProcessor;
	import org.etherframework.common.wire.IWire;
	
	public final class Wire implements IWire
	{
		private var _target:Object;
		private var _ether:IEther;
		private var _processors:Dictionary;
		private var _wired:Boolean = false;
		private var _initialized:Boolean = false;
		
		public function Wire(target:Object)
		{
			_target = target;
			_processors = new Dictionary();
		}
		
		public function initialize(ether:IEther):void
		{
			if (!ether)
				throw new ArgumentError("Argument ether:IEther can not be null.");
			
			_ether = ether;
			_initialized = true;
		}
		
		public function addProcessor(processor:IMetaProcessor,
									 members:Vector.<MetadataMember>):void
		{
			if (_processors[processor])
				throw new Error("Processor " + processor + " is already added.");
			
			_processors[processor] = members;
		}
		
		public function wire():void
		{
			if (!_initialized)
				throw new IllegalOperationError("It should be initialized first.");
			if (_wired)
				throw new IllegalOperationError("Target " + _target + " is already wired.");
			
			for (var processor:Object in _processors)
			{
				processor.wire(_target, _processors[processor], _ether);
			}
			
			_wired = true;
			
			if (_target is INotifiableWirable)
				INotifiableWirable(_target).wired();
		}
		
		public function unwire():void
		{
			if (!_initialized)
				throw new IllegalOperationError("Wire should be initialized.");
			if (!_wired)
				throw new IllegalOperationError("Target " + _target + " is already unwired.");
			
			for (var processor:Object in _processors)
			{
				processor.unwire(_target);
			}
			
			_wired = false;
			
			if (_target is INotifiableWirable)
				INotifiableWirable(_target).unwired();
		}
		
		public function destroy():void
		{
			if (!_initialized)
				throw new IllegalOperationError("Wire should be initialized.");
			
			for (var processor:Object in _processors)
			{
				if (_wired)
					processor.unwire(_target, _processors[processor], _ether);
				
				processor.destroy();
				processor.destroyed = true;
			}
			
			if (_target is INotifiableWirable && _wired)
				INotifiableWirable(_target).unwired();
			
			_target = null;
			_ether = null;
			_processors = null;
			_wired = false;
		}
		
		public function get wired():Boolean { return _wired; }
		public function get ether():IEther { return _ether; }
	}
}